package kr.hkit.barontest;

import com.example.androidtest.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import android.widget.TextView;

public class BaRonMain extends Activity {
	View Outbtn01, InView01, InView02, Outbtn02, regen01, regen02;

	long mBaseTime,mBaseTime2, mPauseTime,mPauseTime2;
	
	final static int IDLE = 0;
	final static int RUNNING = 1;
	final static int PAUSE = 2;
	
	final static int IDLE2 = 0;
	final static int RUNNING2 = 1;
	final static int PAUSE2 = 2;

	int mStatus = IDLE;
	int mStatus2 = IDLE2;
	FrameLayout layout01;
	FrameLayout layout02;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.baron_test);

		layout01 = (FrameLayout) findViewById(R.id.test_layout01);
		layout02 = (FrameLayout) findViewById(R.id.test_layout02);

		Outbtn01 = (Button) findViewById(R.id.out_btn01);
		Outbtn02 = (Button) findViewById(R.id.out_btn02);

		InView01 = (TextView) findViewById(R.id.in_View);
		InView02 = (TextView) findViewById(R.id.in_View2);

		/*
		 * regen01 = (Button) findViewById(R.id.regen_01); regen02 = (Button)
		 * findViewById(R.id.regen_02);
		 */
		InView01.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Outbtn01.setVisibility(View.VISIBLE);
				InView01.setVisibility(View.INVISIBLE);
				// regen01.setVisibility(View.INVISIBLE);

				switch (mStatus) {
				case IDLE:
					mBaseTime = SystemClock.elapsedRealtime();
					mTimer.sendEmptyMessage(0);
					mStatus = RUNNING;
					break;
				case RUNNING:
					mTimer.removeMessages(0);
					mPauseTime = SystemClock.elapsedRealtime();
					mStatus = PAUSE;
					break;
				case PAUSE:
					long now = SystemClock.elapsedRealtime();
					mBaseTime += (now - mPauseTime);
					mTimer.sendEmptyMessage(0);
					mStatus = RUNNING;
					break;
				}

			}
		});
		InView02.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				Outbtn02.setVisibility(View.VISIBLE);
				InView02.setVisibility(View.INVISIBLE);
				// regen02.setVisibility(View.INVISIBLE);
				
				switch (mStatus2) {
				case IDLE2:
					mBaseTime2 = SystemClock.elapsedRealtime();
					mTimer2.sendEmptyMessage(0);
					mStatus2 = RUNNING2;
					break;
				case RUNNING2:
					mTimer2.removeMessages(0);
					mPauseTime2 = SystemClock.elapsedRealtime();
					mStatus2 = PAUSE2;
					break;
				case PAUSE2:
					long now = SystemClock.elapsedRealtime();
					mBaseTime2 += (now - mPauseTime2);
					mTimer2.sendEmptyMessage(0);
					mStatus2 = RUNNING2;
					break;
				}
				

			}
		});
		
		/*BackRunnable runnable = new BackRunnable();
		Thread thread = new Thread(runnable);
		thread.setDaemon(true);
		thread.start();*/
		
	}

	protected void onDestroy() {
		mTimer.removeMessages(0);
		mTimer2.removeMessages(0);
		super.onDestroy();
	}

	Handler mTimer = new Handler() {
		public void handleMessage(Message msg) {
			((TextView) InView01).setText(getInView());
			mTimer.sendEmptyMessage(0);
		}
	};
	
	Handler mTimer2 = new Handler() {
		public void handleMessage(Message msg) {
			((TextView) InView02).setText(getInView());
			mTimer2.sendEmptyMessage(0);
		}
	};

	String getInView() {
		long now = SystemClock.elapsedRealtime();
		long ell = now - mBaseTime;
		String sEll = String.format("%02d:%02d:%02d", ell / 1000 / 60,
				(ell / 1000) % 60, (ell % 1000) / 10);
		return sEll;
	}

	public void mOnClick(View v) {
		Intent intent = null;

		switch (v.getId()) {
		case R.id.out_btn01:
			Outbtn01.setVisibility(View.INVISIBLE);
			InView01.setVisibility(View.VISIBLE);
			// regen01.setVisibility(View.INVISIBLE);

		case R.id.out_btn02:
			Outbtn02.setVisibility(View.INVISIBLE);
			InView02.setVisibility(View.VISIBLE);
			// regen02.setVisibility(View.INVISIBLE);
			break;

		case R.id.listview:
			intent = new Intent(this, Hero.class);
			startActivity(intent);
			break;

		}

	}
	class BackRunnable implements Runnable{

		@Override
		public void run() {
			while(true){
				View Outbtn01, InView01, InView02, Outbtn02, regen01, regen02;
			}
		}
		
	}

}
